package com.ms.oschina.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.News;
import com.ms.oschina.tagpager.NewsFragment;
import com.ms.oschina.ui.activity.NewsDetailsActivity;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.viewholder.SynthesizeViewHolder;

public class NewsAdapter extends PageIndexListAdapter<News> {

	public NewsAdapter(AbsListView listView, List<News> list,
			boolean loadMore) {
		super(listView, list, loadMore);
	}

	@Override
	public void onNormalItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		News news = getListData().get(position);
		// 开启新闻详情界面
		Context context = UiUtils.getContext();
		Intent intent = new Intent(context, NewsDetailsActivity.class);
		intent.putExtra(Constants.NEWSINFODETAILS_ID, news.getId());
		intent.putExtra(Constants.MARK, Constants.MARK_NEWSINFODETAILS);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	public BaseViewHolder<News> getItemViewHolder() {
		return new SynthesizeViewHolder();
	}

	@Override
	public List<News> getLoadMoreData() throws Exception {
		int mark = getMark();
		switch (mark) {
		case NewsFragment.INFORMATION:
			return OsChinaApi.getNewsInformation(pageIndex).getList();
		case NewsFragment.HOTSPOT:
			return OsChinaApi.getNewsHotspot(pageIndex).getList();
		default:
			return null;
		}
	}

}
